package com.peng.leetcode.array;

import java.util.Arrays;
import java.util.HashSet;
import java.util.OptionalInt;
import java.util.Set;

/**
 * FairCandySwap
 * 888. 公平的糖果棒交换
 *
 * @author: lupeng6
 * @create: 2021/2/1 11:18
 */
public class FairCandySwap {

    public static void main(String[] args) {
        int[] A = {1,1};
        int[] B = {2,2};
        System.out.println(new FairCandySwap().fairCandySwap(A, B));

    }

    public int[] fairCandySwap(int[] A, int[] B) {
        int sumA = Arrays.stream(A).sum();
        int sumB = Arrays.stream(B).sum();

        int delete = (sumA - sumB) / 2;
        Set<Integer> set = new HashSet<>();

        Arrays.stream(A).forEach(set::add);

        int[] answer = new int[2];

        OptionalInt first = Arrays.stream(B).filter(a -> set.contains(a + delete)).findFirst();
        int a = first.getAsInt();
        answer[0] = a + delete;
        answer[1] = a;
        return answer;
    }
}
